package com.supermap.originTest;


import com.supermap.data.*;
import com.supermap.data.conversion.DataExport;
import com.supermap.data.conversion.ExportResult;
import com.supermap.data.conversion.ExportSettingKML;
import com.supermap.data.conversion.FileType;
//import org.apache.commons.lang3.RandomStringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 洋小松
 * @date 2024/8/26
 */
public class KmlExport {
	public static void main(String args[]) throws IOException {

		
		String tmp = "RandomStringUtils.randomAlphanumeric(6)";
		String path = "E:\\MapData\\202408\\kmlTest";
		Workspace m_workspace = new Workspace();
		String workspace = path + File.separatorChar + "WorkSpace.smwu";
		WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(workspace);
		conInfo.setType(WorkspaceType.SMWU);
		m_workspace.open(conInfo);

		//创建数据集
		String udbxPath = path + File.separatorChar + tmp + ".udbx";
		String filePath = path + File.separatorChar + tmp + ".kml";
//		File temp = new File(filePath);
//		temp.createNewFile();
		DatasourceConnectionInfo info = new DatasourceConnectionInfo();
		info.setEngineType(EngineType.UDBX);
		info.setAlias("kmlTest");
		info.setServer(udbxPath);
		Datasource datasource = m_workspace.getDatasources().create(info);
		// 设置矢量数据集的信息
		DatasetVectorInfo datasetVectorInfo = new DatasetVectorInfo();
		datasetVectorInfo.setType(DatasetType.REGION);
		datasetVectorInfo.setEncodeType(EncodeType.NONE);
		datasetVectorInfo.setFileCache(true);
		datasetVectorInfo.setName("export");
		//设置属性
		FieldInfo[] fieldInfos = new FieldInfo[1];
		FieldInfo fieldInfo = new FieldInfo();
		fieldInfo.setType(FieldType.CHAR);
		fieldInfo.setName("abc");
		fieldInfo.setCaption("abc");
		fieldInfos[0] = fieldInfo;
		// 创建矢量数据集
		DatasetVector datasetVector = datasource.getDatasets().create(datasetVectorInfo);
		//向数据集中写入数据
		Recordset recordset = datasetVector.getRecordset(false, CursorType.DYNAMIC);
		Geometry geometry = Toolkit.WKTToGeometry("POLYGON((-0.2155 46.4014, -0.1911 46.4075, -0.2026 46.4288, -0.2269 46.4206, -0.2155 46.4014))");
		Map<String,Object> map = new HashMap<>();
		map.put("abc","测试属性值");
		recordset.addNew(geometry,map);
		recordset.update();
		recordset.close();
		//导出数据为kml
		DataExport dataExport = new DataExport();
		ExportSettingKML exportSetting = new ExportSettingKML();
		Dataset dataset = getDataset(datasource);
		exportSetting.setSourceData(dataset);
		exportSetting.setTargetFileType(FileType.KML);
		exportSetting.setOverwrite(true);
		exportSetting.setTargetFilePath(filePath);
		dataExport.getExportSettings().add(exportSetting);
		ExportResult result = dataExport.run();
		System.out.println("export failed count : "+result.getFailedSettings().length);
		datasource.close();
		m_workspace.save();
		m_workspace.dispose();

		
	}

	protected static Dataset getDataset(Datasource datasource){
		Datasets datasets = datasource.getDatasets();
		String[] datasetNames = datasets.getDatasetNames();
		for (int i=0; i< datasetNames.length; i++) {
			Dataset dataset = datasource.getDatasets().get(datasetNames[i]);
			DatasetVector datasetVector = (DatasetVector) dataset;
			if (datasetVector.getRecordCount() > 0){
				return dataset;
			}
		}
		return datasets.get(0);
	}
}
